<?php
/**
 * @auther:周卫星
 * @className:FaqController.php
 * @projectName:train
 * @classDesc:
 * @date:2019/11/20
 * @time:17:55
 */

namespace App\Http\Controllers\admin;


use App\Http\Controllers\Controller;
use App\Model\SysFaq;
use Illuminate\Http\Request;

class FaqController extends Controller
{
    /**
     * 查看帮助信息列表
     * @param Request $request
     * @return FaqController
     */
    public function index(Request $request)
    {
        $model = new SysFaq();
        if ($request -> isMethod('get')){
            if ($request -> header('isAjax') == 'ajax' && $request -> header('type') == 'list'){
                $title = $request -> get('title', '');
                $list = $model -> getSysFaqList($title);
                if ($list['total'] !== 0){
                    $result = [
                        'code' => 0,
                        'message' => '获取成功',
                        'count' => $list['total'],
                        'data' => $list['data']
                    ];
                }else{
                    $result = [
                        'code' => 0,
                        'message' => '获取失败',
                        'count' => 0,
                        'data' => []
                    ];
                }
                return $this -> returnJson($result);
            }else{
                return  view('admin.faq.index');
            }
        }
    }

    /**
     * 添加帮助信息详情
     * @param Request $request
     * @return FaqController|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function add(Request $request)
    {
        if ($request -> isMethod('get')){
            return view('admin.faq.add');
        }
        if ($request -> isMethod('post')){
            $title = $request -> post('title');
            $content = $request -> post('content');
            $data = [
                'title' => $title,
                'content' => $content,
                'addon_at' => time(),
                'update_at' => time()
            ];
            $model = new SysFaq();
            if ($model -> addSysFaq($data)){
                $result = [
                    'code' => 0,
                    'msg' => '添加成功'
                ];
            }else{
                $result = [
                    'code' => 1,
                    'msg' => '添加失败'
                ];
            }
            return $this -> returnJson($result);
        }
    }

    /**
     * 修改帮助信息
     * @param Request $request
     * @return FaqController|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function edit(Request $request)
    {
        $model = new SysFaq();
        if ($request -> isMethod('get')){
            $id = $request -> route('id');
            $info = $model -> getSysFaqInfoById($id);
            return view('admin.faq.edit', ['info' => $info]);
        }
        if ($request -> isMethod('post')){
            $bol = $model -> editSysFaqById($request -> post('id'), ['title' => $request -> post('title'), 'content' => $request -> post('content')]);
            if ($bol){
                $result = [
                    'code' => 0,
                    'msg' => '修改成功'
                ];
            }else{
                $result = [
                    'code' => 1,
                    'msg' => '修改失败'
                ];
            }
            return $this -> returnJson($result);
        }
    }
}
